#include "widget.h"
#include "ui_widget.h"

#include <iostream>
#include <QPainter>
#include <QPixmap>

Widget::Widget(QSize p_size_, QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    p=new QPixmap{p_size_};
    p->fill();
    t=new TD_RBtree;
}

void Widget::draw()
{
    t->all_heights(t->fake_root->right_chl,0);
    QPainter pa(p);
    pa.translate(r,r);
    auto f=pa.font();
    f.setPixelSize(r);
    pa.setFont(f);
    p->fill();
    t->bfs_visit([this,&pa](auto ...l){
        //画线
        auto [x1,y1,x2,y2]=std::tuple(l...);
        pa.drawLine(x1*r,y1*hspace+r,x2*r,y2*hspace+r);
    },[this,&pa](auto ...items){
        //画圆
        auto [x,y,color]=std::tuple(items...);
        if(color==0)
            pa.setBrush(Qt::red);
        else
            pa.setBrush(Qt::black);
        pa.drawEllipse(x*r-r+2,y*hspace+2,2*r-4,2*r-4);
        pa.setBrush(Qt::black);
    },[this,&pa](auto ...items){
        //画文本
        auto [x,y,data]=std::tuple(items...);
        pa.setPen(Qt::white);
        pa.drawText(x*r-r,y*hspace,2*r,2*r,Qt::AlignCenter,QString::number(data));
        pa.setPen(Qt::black);
    });
}

Widget::~Widget()
{
    delete ui;
    delete t;
    delete p;
}

void Widget::fucka()
{

}

void Widget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter pt(this);
    pt.drawPixmap(0,0,*p);
}


void Widget::on_clear_clicked()
{
    p->fill();
    t->empty();
    update();
}

void Widget::on_insert_clicked()
{
//    t->empty();
    auto strlist=ui->lineEdit->text().split(QRegExp("\\W+"),Qt::SkipEmptyParts);
    ui->lineEdit->clear();
    for(int i=0;i<strlist.size();i++){
        auto x=strlist[i].toInt();
        //处理错误
        t->insert(x);
    }
    //8 47 24 4 34 44 32 44 28 26   ->26
    draw();
    update();
    ui->lineEdit->setPlaceholderText("插入完成");
    ui->lineEdit->setFocus();
}

void Widget::on_delete_2_clicked()
{
    auto strlist=ui->lineEdit->text().split(QRegExp("\\W+"),Qt::SkipEmptyParts);
    ui->lineEdit->clear();
    for(int i=0;i<strlist.size();i++)
        t->remove(strlist[i].toInt());
    draw();
    update();
    ui->lineEdit->setPlaceholderText("删除完成");
    ui->lineEdit->setFocus();
}

void Widget::on_lineEdit_editingFinished()
{

}

void Widget::on_randm_insert_clicked()
{
    bool isok=false;
    auto str=ui->lineEdit->text().toInt(&isok);
    ui->lineEdit->clear();
    if(!isok){
        ui->lineEdit->setPlaceholderText("请在当前输入域输入随机数量");
        ui->lineEdit->setFocus();
        return ;
    }
    if(str>200||str<=0){
        ui->lineEdit->setPlaceholderText("数量范围固定在0-200之间，请重新输入");
        ui->lineEdit->setFocus();
        return ;
    }
    srand(time(nullptr));
    int ii=str;
    std::vector<int> a(ii);
    for(int i=0;i<ii;i++)
        a[i]=rand()%500;
    for(int i=0;i<a.size();i++){
        auto x=a[i];
        //处理错误
        t->insert(x);
    }
    draw();
    update();
    ui->lineEdit->setPlaceholderText("随机插入完成");
    ui->lineEdit->setFocus();
}
